﻿using System;
using System.Globalization;
using System.Text;
using System.Web.Services;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.ThongKe
{
    public partial class ThongKeHopDongChuaThanhToan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        [WebMethod]
        public static object HopDongList()
        {
            var hopDongs = new ThongKeBiz().ThongKeHopDongChuaThanhToan();

            return new { Result = "OK", Records = hopDongs };
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            var html = GenerateHtml();
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                string.Format("attachment;filename=Thong_Ke_Hop_Dong_Chua_Thanh_Toan_{0:dd-MM-yyyy_hhmmss}.xls", DateTime.Now));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private string GenerateHtml()
        {
            var hopDongs = new ThongKeBiz().ThongKeHopDongChuaThanhToan();

            var headers = new[]
            {
                "Số HĐ", "Tên HĐ", "Đơn vị", "Giá trị HĐ", "Trước thuế", "Giá trị Quyết toán", "Giá trị Thanh toán nội bộ", "Đã chuyển tiền", "Chưa thanh toán", "Còn lại"
            };

            var sbHtml = new StringBuilder();
            sbHtml.Append("<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");
            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");

            // headers
            sbHtml.Append("<tr>");
            foreach (var header in headers)
            {
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'><strong>{0}</strong></td>", header);
            }
            sbHtml.Append("</tr>");

            if (hopDongs != null)
            {
                foreach (var hopDongView in hopDongs)
                {
                    sbHtml.Append("<tr>");
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:left;'>{0}</td>", hopDongView.SoHopDong);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", hopDongView.TenHopDong);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt;'>{0}</td>", hopDongView.TenDonVi);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.TongGiaTriHopDong == 0 ? null : hopDongView.TongGiaTriHopDong);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.GiaTriHopDongChuaVAT == 0 ? null : hopDongView.GiaTriHopDongChuaVAT);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.GiaTriQuyetToan == 0 ? null : hopDongView.GiaTriQuyetToan);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.GiaTriQuyetToanNoiBo == 0 ? null : hopDongView.GiaTriQuyetToanNoiBo);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.TongSoTienDaThanhToan == 0 ? null : hopDongView.TongSoTienDaThanhToan);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.TongSoTienDaThanhToan == 0 ? null : hopDongView.TongSoTienChuaThanhToan);
                    sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:right;'>{0:N0}</td>",
                        hopDongView.SoTienConLai == 0 ? null : hopDongView.SoTienConLai);

                    sbHtml.Append("</tr>");
                }
            }

            sbHtml.Append("</table>");
            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}